

<template>
<mu-paper :z-depth="1" class="lan-loadmore-body">
  <mu-appbar color="primary" class="lan-header">
      <mu-button icon slot="left" v-close>
        <i class="iconfont angle-left iconangle-left"></i>
        <span class="lan-header-back">返回</span>
      </mu-button>
      质量检查
      <mu-button icon slot="right" >
        <!-- <span class="text">本地保存</span> -->
      </mu-button>
    </mu-appbar>
  <mu-container ref="container" class="lan-loadmore-content no-padding">
    <mu-form :model="{}" class="mu-lan-form" :label-position="'left'" label-width="">
          <mu-form-item label="项目名称" class="form-between" >
            <span class="normal-font-size form-right-text" >{{lineInfo.orgName}}</span>
            <i class="iconfont angle-left iconangle-right"></i>
          </mu-form-item>
          <mu-form-item label="检查类型" class="form-between" >
            <span class="normal-font-size form-right-text" >{{lineInfo.checkTypeName}}</span>
            <i class="iconfont angle-left iconangle-right"></i>
          </mu-form-item>
          <mu-form-item label="所属区域" class="form-between" >
            <span class="normal-font-size form-right-text">{{lineInfo.areaName}}</span>
            <i class="iconfont angle-left iconangle-right"></i>
          </mu-form-item>
          <mu-form-item label="检查部位" class="form-between" >
            <span class="normal-font-size form-right-text">{{lineInfo.checkParts}}</span>
            <i class="iconfont angle-left iconangle-right"></i>
          </mu-form-item>
          <!-- <mu-form-item label="检查部位" class="inline-item">
            <mu-text-field v-model="form.checkParts" multi-line placeholder="请输入" :rows="3" :rows-max="6" ></mu-text-field>
          </mu-form-item> -->
          <div v-if="allList.length && areaDetail.id">
            <showTableopeat v-for="(item,index) in allList" :key="index" v-model="allList[index]" :areaDetail="areaDetail" :projectData="customRoot" ></showTableopeat>
          </div>
         
         
          <!-- 选择的检查指导表 -->
          <!-- <mu-form-item label="" class="column-item" v-show="form.useTable && tableSelectList.length>0" style="padding-bottom:0.4rem;padding-top:0.4rem;">
            <showTable v-for="(item,index) in tableSelectList" :key="index" :data="item.detailInfo"></showTable>
          </mu-form-item> -->
          <mu-form-item label="检查人签名" class="form-between" >
            <span class="normal-font-size form-right-text" @click="opensign">{{signatureList.length?'已签名':'请签名'}}</span>
            <!-- <mu-text-field  placeholder="请输入"></mu-text-field> -->
            <i class="iconfont angle-left iconangle-right"></i>
          </mu-form-item>
          <mu-form-item  class="form-between" v-if="signatureList.length > 0">
            <div v-for="(item,index) in signatureList" :key="index" class="img-box">
              <img :src="item" :preview="index" />
              <mu-icon class="close" value="close" @click="deletesign(index)"></mu-icon>
            </div>
          </mu-form-item>
          <mu-form-item class="form-submit">
            <!-- <mu-button color="primary">重置</mu-button> -->
            <mu-button full-width color="primary" @click="saveData">提交</mu-button>
          </mu-form-item>
        </mu-form>
        
  </mu-container>
  <!-- 签名 -->
  <mu-dialog
      width="100%"
      transition="slide-right"
      fullscreen
      :open.sync="signature"
      class="lan-dialog"
    >
    <div style="height:100%;width:100%;" >
      <Signature v-if="signature"  @close="signature = false" @success="signatureBack"></Signature>
    </div>
  </mu-dialog>

</mu-paper>
</template>

<script>
import showTableopeat from "./checkTemplate/showTableopeat";
import Signature from "@/signature/Signature";
export default {
	name: 'check',
	components: {
    Signature,showTableopeat
  },
  beforeDestroy(){
    sessionStorage.removeItem('checkWorkForm')
  },
  data() {
    return {
      allList:[],
      testObj:{},
      // 上面的几乎没有用处
      signature:false,
      signatureList:[],
      lineInfo:'',
      areaDetail:{},
      customRoot:[], 
      allTable:[],
      checkSignature:'',
      rateList:['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30'],
      
    };
  },
  async created() {
    let nowinfo = this.$A.GS('orgInfo')
     this.customRoot = [{value:nowinfo['orgId'],label:nowinfo['orgName'],type:nowinfo['type']}]
     this.lineInfo = JSON.parse(sessionStorage.getItem('checkListDetail') || null)
    //  this.lineInfo.areaId = '167321639405486486'
     await this.getAreaInfo(this.lineInfo.checkPlanId)
     await this.getProjectUnit(this.lineInfo.areaId)
     console.log(this.lineInfo)
  },
  mounted() {},
  methods: {
    signatureBack(url){
      this.signatureList.push(url)
      this.signature = false;
      this.checkSignature = url;
    },
    opensign(){
      if(this.signatureList.length>0){
        return;
      }
      this.signature = true
    },
    getSomeTable(list = []){
      console.log(list)
      let arr = JSON.parse(JSON.stringify(list));
      let data = [];
      let sq = 0 ;
      for(let i=0; i<arr.length; i++){
        // arr[i]['problemList'] = []
        for(let j=0; j<arr[i]['itemContentList'].length; j++){
          for(let q=0; q<arr[i]['itemContentList'][j]['contentList'].length;q++){
            arr[i]['itemContentList'][j]['contentList'][q]['problemList'] = {}
            arr[i]['itemContentList'][j]['contentList'][q]['has'] = '0'
            arr[i]['itemContentList'][j]['contentList'][q]['sq'] = sq;
            sq ++;
          }
          
        }
        arr[i]['scenePhotos'] = []
        arr[i]['copyUserIds'] = []
        arr[i]['checkResult'] = '0'
        // data.push(arr[i])
      }
      // this.tableSelectList = list
      console.log(arr)
      this.allList = arr;
    },
    //获取区域详情
    async getAreaInfo(id){
      this.$A.Go("get", '/safety/checkPlan/checkTables', {checkPlanId:id}).then(
          response => {
            if (response.code == 200) {
              this.allTable = response.body && response.body.tableList || [];
              console.log(this.allTable)
              this.getSomeTable(this.allTable)
            } else {
              this.$A.MS(response.msg)
            }
          },
          err => {
            console.log(err);
          }
        );
    },






   


    //获取区域所带的责任单位和责任人
    async getProjectUnit(id){//areaDetail
      this.$A.Go("get", '/safety/projectArea/detail', {id}).then(
          response => {
            if (response.code == 200) {
              this.areaDetail = response.body || {};
            } else {
              this.$A.MS(response.msg)
            }
            this.$closeLoading();
          },
          err => {
            this.$closeLoading();
            console.log(err);
          }
        );
    },
    //增加问题
    saveData(){
      let data = this.allList;
      let problemList = [];
      let tablist = [];
      let photoList = [],copyUserList=[];
      for(let i = 0;i<data.length;i++){
        for(let j=0;j<data[i]['itemContentList'].length;j++){
          for(let q=0;q<data[i]['itemContentList'][j]['contentList'].length;q++){
            if(data[i]['itemContentList'][j]['contentList'][q]['has'] == 1){
              problemList.push({
                checkTableId:data[i]['id'],
                checkItemId:data[i]['itemContentList'][j]['itemId'],
                checkContentId:data[i]['itemContentList'][j]['contentList'][q]['contentId'],
                ...data[i]['itemContentList'][j]['contentList'][q]['problemList']
              })
              tablist.push({
                checkTableId:data[i]['id'],
                item:data[i]['itemContentList'][j]['item'],
                itemContent:JSON.stringify([{key:data[i]['itemContentList'][j]['contentList'][q]['contentName'], value:'有问题'}])
              })
            }else{
              tablist.push({
                checkTableId:data[i]['id'],
                item:data[i]['itemContentList'][j]['item'],
                itemContent:JSON.stringify([{key:data[i]['itemContentList'][j]['contentList'][q]['contentName'], value:'无问题'}])
              })
            }
            
          }
        }
        if(data[i]['scenePhotos'].length){
          photoList.push(...data[i]['scenePhotos'])
        }
        if(data[i]['copyUserIds'].length){
          copyUserList.push(...data[i]['copyUserIds'])
        }
      }
       if(problemList.length){//这个有问题
        let quesList2 = problemList
        for(let i =0;i<quesList2.length;i++){
          if(quesList2[i]['problemTypeId']){
            if(quesList2[i]['problemTypeId'] == '-1' && !quesList2[i]['problemElseRemark']){
              this.$A.MS('请填写问题备注')
              return;
            }
          }else{
            this.$A.MS('请选择问题类型')
            return;
          }
          
          if(!quesList2[i]['problemDescription']){
            this.$A.MS('请填写问题描述')
            return;
          }
          if(!quesList2[i]['rectifyDto']['days'] && quesList2[i]['rectifyDto']['days'] != '0'){
            this.$A.MS('请选择整改期限')
            return;
          }
          //???????
          if(!quesList2[i]['scenePhotos'] || !quesList2[i]['scenePhotos'].length){
            this.$A.MS('请上传现场照片')
            return;
          }
          if(!quesList2[i]['rectifyDto']['responsibleUnitId']){
            this.$A.MS('请选择整改单位')
            return;
          }
          
          if(!quesList2[i]['rectifyDto']['responsibleUserId']){
            this.$A.MS('请选择整改人')
            return;
          }
          if(!quesList2[i]['rectifyDto']['rectificationRequirements']){
            this.$A.MS('请填写整改要求')
            return;
          }
          if(!quesList2[i]['rectifyDto']['reviewUser']){
            this.$A.MS('请选择复查人')
            return;
          }
          if(!quesList2[i]['rectifyDto']['rectCopyUserIds'] || !quesList2[i]['rectifyDto']['rectCopyUserIds'].length){
            this.$A.MS('请选择抄送人')
            return;
          }
        }
      }else{//这个表没问题
        //检测几个必填
       
        if(!photoList.length){
          this.$A.MS('请上传图片')
          return;
        }
        if(!copyUserList.length){
          this.$A.MS('请选择抄送人')
          return;
        }
        
      }
      
      let obj = 
        {
          "orgId": this.lineInfo.orgId,  //--项目id
            "areaId": this.lineInfo.areaId, //--区域id
            "checkTypeId": this.lineInfo.checkTypeId, //--检查类型id
            "checkParts": this.lineInfo.checkParts, //--检查部位
            "checkResult": problemList.length?'1':'0', //--检查结果 0无问题 1有问题
            "scenePhotos": problemList.length?'[]':JSON.stringify(photoList), //--现场照片 格式['1','2']
            "checkSignature": this.checkSignature, //--签名
            "id": this.lineInfo.id,
            "checkCopyUserIds": problemList.length?[]:copyUserList, //--检查抄送人
            "problemList": problemList ,//--问题列表
              // {
              //     "checkTableId": "string", --检查表id
              //       "checkItemId": "string", --检查项id
              //   "checkContentId": "string", --检查内容id
              //   "problemTypeId": "string", --问题类型id
              //     "problemElseRemark": "string", --问题类型选则其它时候的备注
              //     "problemLevel": 0, --问题等级 1一般 2较大 3重大
              //     "scenePhotos": "string" --现场照片 格式['1','2']
              //   "floorPlanTag": "string", --平面图标记
              //   "problemDescription": "string", --问题描述
              //   "rectifyDto": {
              //     "days": 0, --整改天数 0立即整改
              //     "endTime": "2019-12-14T06:55:15.580Z", --整改期限
              //     "responsibleUnitId": "string", --整改单位
              //     "responsibleUserId": "string", --整改人
              //     "rectificationRequirements": "string", --整改要求
              //       "reviewUser": "string" --复查人
              //     "isCurrentRect": 0, --问题必传参数 是否现场整改 0是 1否
              //     "rectCopyUserIds": [ --整改抄送人
              //       "string"
              //     ],
              //   },
              // }
            "tableRecList": tablist
      }
     
      this.$openLoading();
      this.$A.Go('post','/safety/checkTask/submit',obj).then(success=>{
        if(success.code == 200){
          this.$A.MS('检查成功')
          this.$router.push('/scuesscheckList');
        }else{
          this.$A.MS(success.msg)
        }
        this.$closeLoading();
      },err=>{
          console.log(err)
          this.$closeLoading();
      })
    },
  }
};
</script>

<style scoped lang='scss' scoped>
.check {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;

	.header {
		color: #888888;
		background-color: #FFFFFF;
	}

  content {
		z-index: 1;
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: auto;
    .mu-tabs {
      background-color: #f5f5f5;
    }
    .demo-text {
      padding: 8px;
      background: #fff;
    }
  }
  
}
.img-box{
    width:0.75rem;
    height:0.75rem;
    margin:0.1rem;
    position: relative;
    img{
      height:100%;
      width:100%;
    }
    .close{
      position: absolute;
      height:0.2rem;
      width:0.2rem;
      line-height:0.2rem;
      font-size:0.15rem;
      top: 0.04rem;
      right: 0.04rem;
      color:#fff;
      border-radius: 50%;
      text-align:center;
      background-color: #FF4242;
    }
  }
</style>
<style scoped lang='scss'>
  .footer-op{
    box-sizing:border-box;
    padding:0.05rem 0;
    margin-top:0 !important;
    overflow:auto;
  }
  .op-texarea{
    .mu-form-item-content{
      .mu-input{
        border:0.01rem solid #e8e8e8;
        border-radius:0.04rem;
        padding:0.05rem;
      }
    } 
  }
 
</style>
